/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:29 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class Hocvien_KiemtraBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Hocvien_Kiemtra Này";
		private static string EX_FK_IHOCVIENID_INVALID="FK_iHocvienID không hợp lệ";
		private static string EX_FK_IKIEMTRAID_INVALID="FK_iKiemtraID không hợp lệ";
		private static string EX_SMATKHAU_EMPTY="sMatkhau không được để trống";
		private static string EX_ITRANGTHAI_INVALID="iTrangthai không hợp lệ";
		private static string EX_ID_INVALID="FK_iHocvienID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Hocvien_Kiemtra Theo ID
        /// </summary>
        /// <param name="FK_iHocvienID">Int64:Hocvien_Kiemtra ID</param>
        /// <returns>Hocvien_KiemtraEntity</returns>        
        public static Hocvien_KiemtraEntity GetOne(Int64 FK_iHocvienID)
        {
            
			if(FK_iHocvienID<=0)
				throw new Exception(EX_ID_INVALID);
            return Hocvien_KiemtraDAL.GetOne(FK_iHocvienID);
        }
        /// <summary>
        /// Lấy về List các Hocvien_Kiemtra
        /// </summary>
        /// <returns>List Hocvien_KiemtraEntity:List Hocvien_Kiemtra Cần lấy</returns>
        public static List<Hocvien_KiemtraEntity> GetAll()
        {
            return Hocvien_KiemtraDAL.GetAll();
        }
        
        /// <summary>
        /// Kiểm tra và thêm mới Hocvien_Kiemtra
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Hocvien_Kiemtra Mới Thêm Vào</returns>
        public static Int32 Add(Hocvien_KiemtraEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return Hocvien_KiemtraDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Hocvien_Kiemtra
        /// </summary>
        /// <param name="entity">Hocvien_KiemtraEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(Hocvien_KiemtraEntity entity)
        {
            checkExist(entity.FK_iHocvienID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return Hocvien_KiemtraDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Hocvien_Kiemtra
        /// </summary>
        /// <param name="FK_iHocvienID">Int64 : FK_iHocvienID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 FK_iHocvienID)
        {
            checkExist(FK_iHocvienID);
            return Hocvien_KiemtraDAL.Remove(FK_iHocvienID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 FK_iHocvienID)
        {
            Hocvien_KiemtraEntity oHocvien_Kiemtra=Hocvien_KiemtraDAL.GetOne(FK_iHocvienID);
            if(oHocvien_Kiemtra==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">Hocvien_KiemtraEntity: entity</param>
        private static void checkLogic(Hocvien_KiemtraEntity entity)
        {   
			if (entity.FK_iHocvienID < 0)
				throw new Exception(EX_FK_IHOCVIENID_INVALID);
			if (entity.FK_iKiemtraID < 0)
				throw new Exception(EX_FK_IKIEMTRAID_INVALID);
			if (String.IsNullOrEmpty(entity.sMatkhau))
				throw new Exception(EX_SMATKHAU_EMPTY);
			if (entity.iTrangthai < 0)
				throw new Exception(EX_ITRANGTHAI_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">Hocvien_KiemtraEntity: Hocvien_KiemtraEntity</param>
        private static void checkDuplicate(Hocvien_KiemtraEntity entity,bool checkPK)
        {
            /* 
            Example
            List<Hocvien_KiemtraEntity> list = Hocvien_KiemtraDAL.GetAll();
            if (list.Exists(
                delegate(Hocvien_KiemtraEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.FK_iHocvienID != entity.FK_iHocvienID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">Hocvien_KiemtraEntity:entity</param>
        private static void checkFK(Hocvien_KiemtraEntity entity)
        {            
        }
        #endregion
    }
}
